package com.example.commonlib.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.example.commonlib.commonutil.MLog;

/**
 * Created by aiyou on 2017/9/28.
 */

public class OnKeyHomeRecevier extends BroadcastReceiver {

    public String SYSTEM_DIALOG_REASON_KEY = "reason";

    public String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

    public String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

    public OnKeyHomeCallBack onKeyHomeCallBack;


    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
            MLog.e("TAG","reason===" + reason);
            if (reason != null) {
                if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                    MLog.e("TAG","Home键被监听");
                    if(onKeyHomeCallBack!=null){
                        onKeyHomeCallBack.downHome();
                    }
                } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                    MLog.e("TAG","多任务键被监听");
                    if(onKeyHomeCallBack!=null){
                        onKeyHomeCallBack.downMenu();
                    }
                }
            }
        }
    }

    public void addOnKeyHomeCallBack(OnKeyHomeCallBack onKeyHomeCallBack){
        this.onKeyHomeCallBack = onKeyHomeCallBack;
    }


    public interface OnKeyHomeCallBack{
        void downHome();
        void downMenu();
    }

}
